package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dao.CategoryDao;
import com.itheima.dao.DishDao;
import com.itheima.dao.DishFlavorDao;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.entity.vo.DishVo;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishDao dishDao;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public IPage<DishVo> getDish(Integer page, Integer pageSize,String name) {

        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.like(name !=null,Dish::getName,name);

        IPage<Dish> dishIPage = new Page<>();
        dishIPage.setPages(page);
        dishIPage.setSize(pageSize);


       IPage<Dish> iPage = dishDao.selectPage(dishIPage, lqw);//源数据

        List<Dish> records = iPage.getRecords();

        List<DishVo> dishVosList = records.stream().map((res) -> {
            DishVo dv = new DishVo();
            Long id = res.getCategoryId();
            Category category = categoryDao.selectById(id);

            dv.setCategoryName(category.getName());
            BeanUtils.copyProperties(res, dv);
            return dv;
        }).collect(Collectors.toList());


        IPage<DishVo> dishVoIPage = new Page<>();
        BeanUtils.copyProperties(iPage,dishVoIPage);
        dishVoIPage.setRecords(dishVosList);

        return   dishVoIPage;
    }

    @Override
    public boolean addDish(DishVo dish) {
        try {
              dishDao.insert(dish);

            List<DishFlavor> flavors = dish.getFlavors();

            for (DishFlavor flavor : flavors) {
                System.out.println(flavors.size());
                flavor.setCreateTime(dish.getCreateTime());
                flavor.setUpdateTime(dish.getUpdateTime());
                flavor.setCreateUser(dish.getCreateUser());
                flavor.setUpdateUser(dish.getUpdateUser());
                flavor.setDishId(dish.getId());
             dishFlavorService.addDishFlavor(flavor);
            }
        } catch (Exception e) {
            System.out.println( e.getMessage());
            return false;
        }

        return true;
    }

    @Override
    public DishVo getByIdDish(Long id) {
        //根据菜品查询口味表中有没有数据
        List<DishFlavor> flavorList = dishFlavorService.getByIdFlavor(id);


        Dish dish = dishDao.selectById(id);

        DishVo dishVo1 = new DishVo();
        BeanUtils.copyProperties(dish,dishVo1); //拷贝
        dishVo1.setFlavors(flavorList);

        System.out.println("-------------------------"+dishVo1);
        return dishVo1;
    }

    @Override
    public boolean updateDish(DishVo dishVo) {

        try {
            LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();

            lqw.eq(Dish::getId,dishVo.getId());
            dishDao.update(dishVo,lqw);
            List<DishFlavor> flavors = dishVo.getFlavors();
            if (flavors.size() > 0 ){
                //直接先删除，然后再添加
                dishFlavorService.deleteByDishIdDishFlavor(dishVo.getId());
                for (DishFlavor flavor : flavors) {
                    System.out.println(flavors.size());
                    flavor.setCreateTime(dishVo.getCreateTime());
                    flavor.setUpdateTime(LocalDateTime.now());
                    flavor.setCreateUser(dishVo.getCreateUser());
                    flavor.setUpdateUser(dishVo.getUpdateUser());
                    flavor.setDishId(dishVo.getId());
                    dishFlavorService.addDishFlavor(flavor);
                }


            }
            else{
                dishFlavorService.deleteByDishIdDishFlavor(dishVo.getId());
                for (DishFlavor flavor : flavors) {

                    dishFlavorService.addDishFlavor(flavor);
                }
            }
        } catch (Exception e) {
            return false;
        }


        return true;
    }

    @Override
    public boolean updateStatus(List<Long> ids,Integer status) {
        try {
            LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
            lqw.in(Dish::getId,ids);
            Dish dish = new Dish();
            dish.setStatus(status);
            dishDao.update(dish, lqw);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteDish(List<Long> ids) {

        try {
            List<Dish> byIdDish = getByIdDish(ids);

            for (Dish statusDish : byIdDish) {
                if (statusDish.getStatus() == 1) {
                    LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
                    lqw.in(Dish::getId, ids);
                    dishDao.delete(lqw);

                }
                else{
                    return false;
                }
            }
        } catch (Exception e) {
            return  false;
        }


        return true;
    }

    @Override
    public List<Dish> getByIdDish(List<Long> ids) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(Dish::getId,ids);

        List<Dish> dishes = dishDao.selectList(lqw);

        return dishes;
    }

//    @Override
//    public List<Dish> getByCategoryId(Long categoryId,String name) {
//
//        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(categoryId!=null, Dish::getCategoryId,categoryId);
//        lqw.like(name!=null,Dish::getName,name);
//
////        List<Dish> dishList = dishDao.selectList(lqw);
//
//          return dishDao.selectList(lqw);
//    }


    @Override
    public List<DishVo> getByCategoryId(Long categoryId,String name) {

        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(categoryId!=null, Dish::getCategoryId,categoryId);
        lqw.like(name!=null,Dish::getName,name);

        List<Dish> dishList = dishDao.selectList(lqw);

        List<DishVo> collect = dishList.stream().map((dish) -> {
            //口味集合
            List<DishFlavor> dishFlavor = dishFlavorService.getByIdFlavor(dish.getId());

            DishVo dishVo = new DishVo();

            dishVo.setFlavors(dishFlavor);

            BeanUtils.copyProperties(dish,dishVo);

         return dishVo;
        }).collect(Collectors.toList());

        System.out.println(collect);


        return collect;
    }


}
